package fr.mywiki.business.entry.meta;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.jdom.Document;
import org.jdom.Element;

import fr.mywiki.model.ejbAdapter.Value;

/**
 * A EntryModel object is the model of an entry, the way to represent it.
 */
public class EntryModel extends Value {
	private static final long serialVersionUID = 1L;

	public static final String TYPE_ENT = "entrymodel";
	
	/* The fields of this object */
	public static final String FIELD_NAME = "name"; // Name of the model
	public static final String FIELD_DESCRIPTION = "description"; // description
	
	/* The relations of this object */
	public static final String LINK_ENTRY_MODEL = Property.LINK_ENTRY_MODEL;
	
  /** The name of this model */
  private String name;

  /**
   * The properties constituting this structure model. A <code>HashMap</code> of
   * <code>String</code>s (the names of the properties) linked to
   * <code>Property</code> objects.
   */
  private HashMap properties;

  public EntryModel() {
  	properties = new HashMap();
  }
  
  public EntryModel(String name) {
  	this();
  	this.name = name;
  }

  public Property getProperty(String name) {
    return (Property)properties.get(name);
  }
  
  /**
   * Returns an iterator on the properties.
   */
  public Iterator iterateProperties() {
  	return properties.values().iterator();
  }
  
  /**
   * Adds a new <code>Property</code> object to the set of properties contained
   * in that entry model.
   * 
   * @param prop
   *          a <code>Property</code> object.
   * @throws ModelException if the added property's name already exists in this model.
   */
  public void addProperty(Property prop) throws ModelException {
    String name = prop.getName();
    if (getProperty(name) != null)
      throw new ModelException(
          "A property with this name already exists for this model.");
    properties.put(name, prop);
  }
  
  public void removeProperty(String name) {
  	properties.remove(name);
  }
  
  public void removeAllProperties() {
  	properties = new HashMap();
  }

  /**
   * Returns a copy of the set of properties of this model.
   * 
   * @return a <code>Set</code> of <code>Property</code> objects.
   */
  public Set getProperties() {
    return new HashSet(properties.values());
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

	public String getTypeEnt() {
		return TYPE_ENT;
	}

	public Object get(String attName) {
		if(FIELD_NAME.equals(attName))
			return getName();
		return null;
	}

	public void set(String attName, Object value) {
		// Void one...
	}
	
	/**
	 * Renders a xml representation of this model (in order to be put inside the database.
	 * 
	 * @return a <code>Document</code> object.
	 */
	public Document toXml() {
		Element elt = new Element(TYPE_ENT);
		
		Iterator itProps = properties.values().iterator();
		while (itProps.hasNext()) {
			elt.addContent(((Property) itProps.next()).toXml());
		}
		
		return new Document(elt);
	}
}
